#include "task.h"

static unsigned char cntr_soc = 0;
//static unsigned char cntr_led = 0;
static unsigned char cntr_key = 0;
static unsigned char cntr_dsp = 0;
static unsigned char cntr_tmp = 0;
static unsigned char cntr_ctr = 0;
static unsigned int  cntr_5s = 0;

static unsigned char ui = 0;
static unsigned int v_t = 0;
static unsigned int distance = 0;
static unsigned char para_t = 23;	//默认温度参数:23℃
static unsigned char para_m = 0;	//默认温度控制模式:0

static unsigned char para_up = 45, para_down = 5;
static unsigned char para_select = 0;
static unsigned int cntr_1s = 0;
static unsigned int frq = 0;	//频率
static unsigned char hum = 0;	//湿度

bit f_hz_khz = 0;	//频率单位模式切换
bit f_cm_m = 0;		//距离单位模式切换

bit f_rtc = 0;
bit f_col = 0;
bit f_hms = 0;	//时分秒显示模式
static unsigned char para_id = 0;	//默认频率参数
static unsigned char para_h = 40;	//原值 - 10 -60
static unsigned char para_l = 6;	//放大10倍	0.1 - 1.2
static unsigned char para_f = 90;	//放大10倍	1.0 - 12.0

//1ms中断一次@12MHz
void initSysTick(void)
{
    AUXR |= 0x04;
    T2L = 0x20;
	T2H = 0xD1;
	AUXR |= 0x10;
    IE2 |= 0x04;
	EA = 1;
}


//初始化板级外设
void initBsp(void)
{
	BUZ(0x00);
	LED(0xFF);
	InitDsp();
	initRTCStruct();
	InitKeyStruct();
	InitPCF8591();	
	InitSonicTimer();
	InitFrqTimer();
}

//定时器中断服务函数
void isrSysTick(void)    interrupt 12
{
	GetDisplayStruct()->SegDisplay();
	
	if(cntr_tmp < 200){
		++cntr_tmp;
	}

	if(cntr_soc < 200){
		++cntr_soc;
	}
	if(cntr_key < 10){
		++cntr_key;
	}

	if(cntr_dsp < 50){
		++cntr_dsp;
	}
	if(cntr_ctr < 100){
		++cntr_ctr;
	}
	
	if(cntr_5s > 0){
		--cntr_5s;
	}
	
	if(cntr_1s < 1000){
		++cntr_1s;
	}
}

void Delay100us()		//@12.000MHz
{
	unsigned char i, j;

	i = 2;
	j = 39;
	j = 16;
	do
	{
		while (--j);
	} while (--i);
}

//
static unsigned char led = 0xff;
static unsigned char buz = 0x00;

void taskControl(void)
{
	static unsigned int buz_counter = 0;
	static unsigned char key_counter = 0;
	
	//100ms 刷新
	if(cntr_ctr < 100)
		return;
	cntr_ctr = 0;
	
	//LED控制
	if(ui == 0){
		led &= ~(1 << 0);
		led |= (1 << 1);
		led |= (1 << 2);
	}
	else if(ui == 1){
		led &= ~(1 << 1);
		led |= (1 << 0);
		led |= (1 << 2);
	}
	else if(ui == 2){
		led &= ~(1 << 2);
		led |= (1 << 0);
		led |= (1 << 1);
	}
	else{
		if(para_id == 0){
			led ^= (1 << 0);
			led |= (1 << 1);
			led |= (1 << 2);;
		}
		else if(para_id == 1){
			led ^= (1 << 1);
			led |= (1 << 0);
			led |= (1 << 2);
		}
		else {
			led ^= (1 << 2);
			led |= (1 << 0);
			led |= (1 << 1);
		}
	}
	
	//DAC输出
	if(v_t/5 < para_h){
		SetDAC((1.0/5.0)*255);
	}else if(v_t/5 > 80){
		SetDAC((5.0/5.0)*255);
	}else{
		SetDAC(255 * ((v_t/5) * 4.0/(80 - para_h) + 1.0 - (4.0*para_h)/(80 - para_h)) / 5);
	}
	
	
	if(v_t /5 > para_h){
		led &= ~(1 << 4);
	}else{
		led |= (1 << 4);
	}
	
	//继电器
	if(distance > para_l*10){
		
		led &= ~(1 << 5);
		if(buz & (1 << 4)){
			;		
		}else{
			WriteAT24C02(0, ++buz_counter);
			buz |= (1 << 4);
		}
	}else{
		if(buz & (1 << 4)){
			buz &= ~(1 << 4);
			WriteAT24C02(0, ++buz_counter);
		}else{
			;
		}
		
		led |= (1 << 5);
	}
	
	//脉冲输出
	if(frq > para_f*100){
		led &= ~(1 << 3);
		
	}else{
		led |= (1 << 3);
	}
	
	//长按清零
	if(ui == 1){
		if(P30 == 0){
			if(++key_counter >= 8){
				key_counter = 0;
				buz_counter = 0;
				WriteAT24C02(0,0);
			}
		}else{
			key_counter = 0;
		}
	}else{
		key_counter = 0;
	}
	
	LED(led);
	BUZ(buz);
}

void pwm(unsigned char value)
{
	static unsigned char index = 0;
	//80%
	if(value){
		
		if(++index < value){
			buz |= (1 << 5);
		}else{
			buz &= ~(1 << 5);
		}
		Delay100us();
		
		if(index == 10){
			index = 0;
		}
		
	}
	BUZ(buz);
}

//
void taskCollect(void)
{
	/* 采集电压数据  */
	if(cntr_tmp == 200){
		cntr_tmp = 0;
		v_t = (unsigned int)(GetADC(3) * 500.0 / 255);
	}	
	
	/* 采集距离数据 */
	if(cntr_soc == 200){
		cntr_soc = 0;
		distance = CalDistance();
	}	
    if(cntr_key == 10){
		cntr_key = 0;
        GetKeyStruct()->pFun();
    }
	
	/* 采集频率数据 */
	if(cntr_1s == 1000){
		cntr_1s = 0;
		frq = CalFrq();
	}
	
	if(frq > para_f*100){
		pwm(9);
	}else{
		pwm(3);
	}
}

//
void taskKey(void)
{
	//按键部分处理
	switch(GetKeyStruct()->value){
		case 4:
			if(++ui == 4){
				ui = 0;
			}
			if(ui == 3){
				para_id = 0;	//默认频率参数
			}
			GetKeyStruct()->value = 255;
			break;
		case 5:
			/* 参数界面下，调整参数切换 */
			if(ui == 3){
				if(++para_id == 3){
					para_id = 0;	//
				}
			}
			GetKeyStruct()->value = 255;
			break;
		case 6:
			/* 参数设置 - 加*/
			if(ui == 3){
				if(para_id == 0){
					
					para_f += 5;
					if(para_f > 120){
						para_f = 10;;
					}
				}
				else if(para_id == 1){
					para_h += 10;
					if(para_h > 60){
						para_h = 10;
					}
				}
				else{
					para_l += 1;
					if(para_l > 12){
						para_l = 1;
					}
				}
			}
			/*切换单位*/
			if(ui == 2){
				f_cm_m = ~f_cm_m;
			}
			GetKeyStruct()->value = 255;
			break;
		case 7:
			/* 参数设置 - 减*/
			if(ui == 3){
				if(para_id == 0){
					
					para_f -= 5;
					if(para_f < 10){
						para_f = 120;;
					}
				}
				else if(para_id == 1){
					para_h -= 10;
					if(para_h < 10){
						para_h = 60;
					}
				}
				else{
					para_l -= 1;
					if(para_l < 1){
						para_l = 12;
					}
				}
			}
			/* 切换单位*/
			if(ui == 0){
				f_hz_khz = ~f_hz_khz;
			}
			GetKeyStruct()->value = 255;		
			break;
		default:
			GetKeyStruct()->value = 255;			
			break;
	}
}

//显示处理
void taskDisplay(void)
{
	unsigned int temp = 0;
	
	DisplayStruct* seg = GetDisplayStruct();
	RTC *rtc = getRTCStruct();
	
	//显示部分处理
	if(cntr_dsp < 50){
		return;
	}	
	cntr_dsp = 0;
	
	switch(ui){
		/* 频率界面数据处理 */
		case 0:
			if(f_hz_khz == 0){
				seg->dot = 255;
				seg->buf[0] = 16;	
				seg->buf[1] = 10;	
				seg->buf[2] = 10;	
				seg->buf[3] = (frq > 9999) ?(frq / 10000):(10);	
				seg->buf[4] = (frq > 999) ?(frq % 10000 / 1000):(10);	
				seg->buf[5] = (frq > 99) ?(frq % 1000 / 100):(10);		
				seg->buf[6] = (frq > 9) ?(frq % 100 / 10):(10);	;	
				seg->buf[7] = frq % 10;	
			}else{
				temp = (unsigned int)(frq / 100.);
				seg->dot = 6;
				seg->buf[0] = 16;	
				seg->buf[1] = 10;	
				seg->buf[2] = 10;	
				seg->buf[3] = 10;	
				seg->buf[4] = 10;	
				seg->buf[5] = (temp > 99) ?(temp % 1000 / 100):(10);	
				seg->buf[6] = temp % 100 / 10;	
				seg->buf[7] = temp % 10;	
			}
			break;
		/* 湿度显示界面数据处理 */
		case 1:
			seg->dot = 255;
			seg->buf[0] = 17;
			seg->buf[1] = 10;
			seg->buf[2] = 10;
			seg->buf[3] = 10;
			seg->buf[4] = 10;
			seg->buf[5] = 10;
			seg->buf[6] = v_t / 5 / 10;
			seg->buf[7] = v_t / 5 % 10;
			break;
		/* 距离显示界面数据处理 */
		case 2:
			if(f_cm_m == 0){
				seg->dot = 255;
				seg->buf[5] = (distance > 99)?(distance/100):(10);
				seg->buf[6] = (distance > 9)?(distance%100/10):(10);
				seg->buf[7] = distance % 10;	
			}else{
				seg->dot = 5;
				seg->buf[5] = distance/100;
				seg->buf[6] = distance%100/10;
				seg->buf[7] = distance % 10;				
			}
			seg->buf[0] = 15;
			seg->buf[1] = 10;
			seg->buf[2] = 10;		
			seg->buf[3] = 10;
			seg->buf[4] = 10;
			

			break;		
		/* 参数显示界面数据处理 */
		case 3:
			seg->buf[0] = 13;
			seg->buf[1] = para_id + 1;	
			seg->buf[2] = 10;
			seg->buf[3] = 10;
			seg->buf[4] = 10;
			
			//频率参数
			if(para_id == 0){
				seg->dot = 6;	
				seg->buf[5] = (para_f > 99)?(para_f / 100):(10);
				seg->buf[6] = para_f % 100 /10;
				seg->buf[7] = para_f % 10;
			}
			//湿度参数
			else if(para_id == 1){
				seg->dot = 255;					
				seg->buf[5] = 10;
				seg->buf[6] = para_h / 10;
				seg->buf[7] = para_h % 10;
			}
			//距离参数
			else{
				seg->dot = 6;					
				seg->buf[5] = 10;
				seg->buf[6] = para_l / 10;
				seg->buf[7] = para_l % 10;
			}
			break;

	}	
}